<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-title">
        <h2>用户注册</h2>
      </div>
      
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" show-password></el-input>
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="registerForm.realName" placeholder="请输入真实姓名"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="full-width" :loading="loading" @click="handleRegister">注册</el-button>
        </el-form-item>
        
        <div class="register-options">
          <span>已有账号？</span>
          <router-link to="/login">返回登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

export default {
  name: 'Register',
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    
    // 注册表单
    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: '',
      email: '',
      phone: '',
      realName: ''
    })
    
    // 验证密码是否一致
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    // 验证手机号码
    const validatePhone = (rule, value, callback) => {
      const pattern = /^1[3-9]\d{9}$/
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (!pattern.test(value)) {
        callback(new Error('请输入有效的手机号码'))
      } else {
        callback()
      }
    }
    
    // 表单验证规则
    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能小于 6 个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, validator: validateConfirmPassword, trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ],
      phone: [
        { required: true, validator: validatePhone, trigger: 'blur' }
      ],
      realName: [
        { required: true, message: '请输入真实姓名', trigger: 'blur' }
      ]
    }
    
    const registerFormRef = ref(null)
    const loading = ref(false)
    
    // 处理注册
    const handleRegister = () => {
      //validate就是触发表单的验证功能
      registerFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            const res = await userStore.registerAction(registerForm)
            ElMessage.success('注册成功，请登录')
            router.push('/login')
          } catch (error) {
            console.error('注册失败:', error)
          } finally {
            loading.value = false
          }
        } else {
          return false
        }
      })
    }
    
    return {
      registerForm,
      registerRules,
      registerFormRef,
      loading,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../assets/back.png');
  /* background-image: url("https://preview.qiantucdn.com/58pic/x7/6T/At/HI/cav46zq83miys1wtp7b2rjlofk9g5nxh_PIC2018_png_aiys_PIC2018.jpg%21qt_h320"); */
  background-size: cover;
  background-repeat: no-repeat;
}

.register-box {
  width: 400px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px); /* 毛玻璃效果 */
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
}

.register-title h2 {
  font-size: 24px;
  /* 修改文字颜色为白色，在透明背景下更清晰 */
  color: white;
  margin: 0;
  /* 可选：添加文字阴影增强可读性 */
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}

.register-options {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
  /* 修改文字颜色为白色 */
  color: white;
}

.register-options a {
  color: #a0cfff; /* 使用更亮的蓝色在透明背景下更清晰 */
  text-decoration: none;
}

/* 如果需要，可以添加样式来调整表单元素的颜色 */
:deep(.el-form-item__label) {
  color: white !important;
}

:deep(.el-input__inner) {
  background-color: rgba(255, 255, 255, 0.9) !important;
}

:deep(.el-button--primary) {
  background-color: rgba(64, 158, 255, 0.9) !important;
  border-color: rgba(64, 158, 255, 0.9) !important;
}
</style>